import { basicRouter } from "../../config/basicRouter";
import { lazy } from "react";
import { getRoutes } from "..";
import { getStorage } from "@/utils/utils";
import { TOKEN_NAME } from "@/config";
import { useNavigate } from "react-router-dom";
const Layout = lazy(() => import("@/layouts/index"));
const Empty = lazy(() => import("@/views/empty"));
export const authRouter = (routes: any[]) => {
  routes.push(basicRouter());
  if (getStorage(TOKEN_NAME)) {
    routes.unshift({
      path: "/",
      element: <Layout />,
      children: [
        ...getRoutes(),
        {
          path: "*",
          element: <Empty />,
        },
      ],
    });

    if (location.pathname == "/login") {
      window.history.back();
    }
    if (location.pathname == "/") {
      console.log(getRoutes());

      window.location.replace(getRoutes()[0].path);
    }
  } else {
    if (location.pathname !== "/login") {
      window.location.replace("/login");
    }
  }

  return routes;
};
